package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;

import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.CommonXYLayoutPolicy;
import edu.pku.sei.transformation.editor.internal.TransformationEditorPaletteFactory;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.execution.TransformationExecution;
import edu.pku.sei.transformation.editor.views.figures.TransformationExecutionFigure;

public class TransformationExecutionEditpart extends NodeEditpart {

	public TransformationExecutionEditpart(MetaShapeElement model) {
		super(model);
	}
	@Override
	protected IFigure createFigure() {
		TransformationExecutionFigure figure =  new TransformationExecutionFigure();
		TransformationExecution te = (TransformationExecution) getNode().getModelElement();
		figure.setName(te.toString());
		if (getNode().getDiagram().getType().equals(TransformationEditorPaletteFactory.TRANSFORMATION_TEST)) {
			figure.setTag("<<Test>>");
		} else if (getNode().getDiagram().getType().equals(TransformationEditorPaletteFactory.TRANSFORMATION_COMPOSITE_DIAGRAM)
				|| getNode().getDiagram().getType().equals(TransformationEditorPaletteFactory.TRANSFORMATION_EXECUTION)) {
			figure.setTag("<<Execution>>");
		} else {
			figure.setTag("");
		}
		return figure;
	}

	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		TransformationExecutionFigure figure = (TransformationExecutionFigure) getFigure();
		TransformationExecution execution = (TransformationExecution) getNode().getModelElement();
		if (ModelPackage.TRANSEXE_SPECIFICATION.equals(propertyName)) {
			figure.setName(execution.toString());
		} else if (ModelPackage.NAMEDELEMENT_NAME.equals(propertyName)) {
			figure.setName(execution.toString());
		} else {
			super.handlePropertyChanged(event);
		}
	}
	
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new CommonXYLayoutPolicy());
	}

}
